package com.iman.Confucius.web.student;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.iman.Confucius.service.CodeNameService;
import com.iman.Confucius.service.student.CaseService;
import com.iman.Confucius.vo.CaseVo;
import com.iman.Confucius.vo.CodeName;
import com.iman.Confucius.web.BaseController;
import com.iman.common.support.Page;
import com.iman.common.util.ConfigUtil;

 
@Controller
@RequestMapping(value="/student/case/*")
/**
 * *************************************************************************
 * @文件名称: MyCaseController.java
 *
 * @包路径  : com.iman.Confucius.web.student 
 *				 
 * @版权所有:   TSW   科技有限公司 (C) 2014
 *
 * @类描述:    我的案例分析
 * 
 * @创建人:   wangxiaolei  
 *
 * @创建时间: 2014年12月18日 - 上午1:54:58 
 *
 * @修改记录:
   -----------------------------------------------------------------------------------------------
             时间						|		修改人		|		修改的方法		|		修改描述                                                                
   -----------------------------------------------------------------------------------------------
							|					|					|                                       
   ----------------------------------------------------------------------------------------------- 	
 
 **************************************************************************
 */
public class MyCaseController  extends BaseController {
	
  
	@Autowired
	private CaseService casesServiceImpl;
	
	@Autowired
	private CodeNameService codeNameServiceImpl;
	
	/**
	 * 
	 *  Function:
	 *  功能说明：
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2014年12月16日 下午2:59:38
	 *	返回类型: ModelAndView    
	 *  @param request
	 *  @param menu
	 *  @return
	 */
	@RequestMapping(value="/init")
	public ModelAndView initMyCase(HttpServletRequest request){
		Map<String, Object> model = new HashMap<String, Object>();			
		model.put("ps", 10);
		if(request.getParameter("pageNo")==null ||request.getParameter("pageNo").equals("") ){
			model.put("pn", 1);	
				}
		else{
			model.put("pn", request.getParameter("pageNo"));
		}
		if(request.getParameter("case_tp") == null || request.getParameter("case_tp").equals("") || request.getParameter("case_tp").equals("all"))		{
			
		}else	{
			model.put("case_tp",  request.getParameter("case_tp"));
		}
		
		model.put("userid", getUserIdInSession(request).toString() ); 
		
		Page<CaseVo>  vos  =casesServiceImpl.getCasePage(model);  
		 
		ModelAndView view =null;
		view = new ModelAndView("student/my_case",model);
		putResourceSrc( view);
		view.addObject("pageData" , vos);
		if(request.getParameter("case_tp") == null || request.getParameter("case_tp").equals("") || request.getParameter("case_tp").equals("all")){
			
		}
		else	{
			view.addObject("case_tp",  request.getParameter("case_tp"));
		}
		
		 
		//获取案例分类
		Map<String, Object> maptype = new HashMap<String, Object>();
		//todo:share_case改为常量
		maptype.put("code_gp", "share_case");
		List<CodeName> codenames = codeNameServiceImpl.getCodeNames(maptype);
		view.addObject("codename",codenames);
		view.addObject("url", ConfigUtil.getString( "drmserverUrl" ));
				
		return view;
	}
	
	/**
	 * 添加案例之前，接受 案例分享告知书中的内容
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/accept")
	public ModelAndView acceptMyCase(HttpServletRequest request,String id){ 
		ModelAndView view =null;		 
			Map<String, Object> model = new HashMap<String, Object>();		
			model.put("case_id", request.getParameter("case_id"));
			view = new ModelAndView("student/my_case_add",model);
			model.put("code_gp", "share_case");
			List<CodeName> codenames = codeNameServiceImpl.getCodeNames(model);
			view.addObject("codename",codenames);
			if( request.getParameter("case_id")!= null )
			{
				CaseVo  casevo = new CaseVo();
				  casevo= casesServiceImpl.getDetail(model);		
				  view.addObject("casevo",casevo);
			}
			
		return view;
	}
	
	private String getUTF8code(String value)
	{
		try {
			return new String(value.getBytes("ISO-8859-1"),"utf8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return value;
		}
	}
	
	/**
	 * 保存案例信息
	 * @param request
	 * @param id
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/add")
	@ResponseBody
	public Map<String, Object> addMyCase(HttpServletRequest request,String id) throws UnsupportedEncodingException{
		Map<String, Object> model = new HashMap<String, Object>();		
		model.put("tilte", request.getParameter("title"));
		model.put("case_tp", request.getParameter("case_tp"));
		model.put("case_visitor", request.getParameter("case_visitor"));		
		model.put("case_visitor_say", request.getParameter("case_visitor_say"));
		model.put("case_handle_content", request.getParameter("case_handle_content"));		
		model.put("case_cure_result",request.getParameter("case_cure_result"));
		
		 model.put("user_id", getUserIdInSession(request) ) ;
		 if(request.getParameter("case_id") == null || request.getParameter("case_id").equals("")) {			
			 int result=casesServiceImpl.addCase(model); 
		 }
		 else {
			 model.put("case_handle_content_id", request.getParameter("case_handle_content_id") );
			 model.put("case_cure_result_id",request.getParameter("case_cure_result_id") );
			 model.put("case_id",request.getParameter("case_id") ) ;
			 int result=casesServiceImpl.editCase(model);
		 }
		 //操作成功后刷新页面		
		//return initMyCase(request);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", "success");
		return map;
	}
	
	
	/**
	 * 删除案例信息
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="/del")
	public ModelAndView delMyCase(HttpServletRequest request,HttpServletResponse response) throws IOException{ 
		Map<String, Object> model = new HashMap<String, Object>();		
		model.put("case_id", request.getParameter("caseid"));
		int result=casesServiceImpl.delCase(model);   
		JSONObject json = new JSONObject();
		json.put("result", result);
	 	 
		response.setContentType("text/html;charset=utf-8");
		PrintWriter print = response.getWriter();
		print.print(json);
		print.flush();
		print.close();
		return null;
	} 
	
	
	@RequestMapping(value="/get")
	public ModelAndView getMyCase(HttpServletRequest request,HttpServletResponse response) throws IOException{ 
		Map<String, Object> model = new HashMap<String, Object>();		
		model.put("case_id", request.getParameter("caseid"));
		CaseVo  casevo = new CaseVo();
		  casevo= casesServiceImpl.getDetail(model);		
		JSONObject json = new JSONObject();
	
		json.put("result", casevo);
	 	 
		response.setContentType("text/html;charset=utf-8");
		PrintWriter print = response.getWriter();
		print.print(json);
		print.flush();
		print.close();
		return null;
	} 
}
